﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_ep_real_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            //string strId = Request["id"].ToString();
            if (Request["id"]!=null)
            {
                //修改
                string strId = Request["id"].ToString();
                this.hid_id.Value = strId;
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                CMSModel.ePioneer_Real epr = null;
                int rid = Convert.ToInt32(this.hid_id.Value);
                epr = dal.ePioneer_Real.First(x => x.id == rid);
                this.txt_name.Text = epr.name;
                this.Image1.ImageUrl = epr.img;
                this.txt_brief.Text = epr.brief;
                this.txt_reason.Text = epr.reason;
                this.txt_step.Value = epr.step.Replace("<br>", "\r\n");
                this.txt_case.Text = epr.realcase;
            }
            else
            {
                this.hid_id.Value = "0";
            }
           
        }
    }
    protected void BtnModify_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.ePioneer_Real epr = null;
        string imgPath = System.Configuration.ConfigurationManager.AppSettings["imgPath"].ToString();      //imgpath
        string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"].ToString();       //filepath
        string strImgPath = "";
        if (FileUpload1.HasFile)
            {
                var data = FileUpload1.FileBytes;
                var fileName = FileUpload1.FileName;
                string msg = "";
                string path = filePath + "real/" + fileName;          
                strImgPath = imgPath + "real/" + fileName;    
                Deliver.AsynchronousClient.StartTranCommand(data, path, out msg);
               

            }


        if (this.hid_id.Value != "0")
        {
            int rid = Convert.ToInt32(this.hid_id.Value);
            epr = dal.ePioneer_Real.First(x => x.id == rid);
            if (strImgPath != "")
            {
                epr.img = strImgPath;
            }
            epr.name = this.txt_name.Text.Trim();

            epr.brief= this.txt_brief.Text.Trim();
            epr.reason=this.txt_reason.Text.Trim();
            epr.step = this.txt_step.Value.Trim().Replace("\r\n", "<br>");
            epr.realcase=this.txt_case.Text.Trim();

            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='ep_real.aspx'", true);
        }
        else
        {
            dal.ePioneer_Real.AddObject(new CMSModel.ePioneer_Real
            {
               name=this.txt_name.Text,
               img = FileUpload1.FileName,
               brief= this.txt_brief.Text.Trim(),
               reason=this.txt_reason.Text.Trim(),
               step = this.txt_step.Value.Trim(),
               realcase=this.txt_case.Text.Trim()
            });
            try
            {
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('新增成功');document.location='ep_real.aspx'", true);
            }
            catch (Exception ex)
            { }

           
        }
    }
}